/*
 * SaleHandler.java
 *
 * Created on March 28, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E;

import edu.byu.isys413.group1E.data.*;
import java.sql.SQLException;

/**
 *
 * @author Kendall Frazier
 */
public class SaleHandler {
    
    private static SaleHandler instance = null;
    
    /** Creates a new instance of SaleHandler */
    public SaleHandler() {
    }
    
    /**
     *gets you an instance of saleHanlder 
     *
     * @return instance of saleHandler
     */
    public static synchronized SaleHandler getInstance() {
        if (instance == null) {
            instance = new SaleHandler();
        }
        return instance;
    }
    
    /**
     *creates a transaction with a blank sale 
     *
     * @throws java.lang.Exception 
     * @return 
     */
    public TransLineBO createTXLineWithSale()throws Exception{
        TransLineDAO TXLineDAO = TransLineDAO.getInstance();
        
        TransLineBO TXLine = TXLineDAO.create();
        
        RSDAO_Factory factory = RSDAO_Factory.getInstance();
        RevenueSourceDAO revSrcDAO = factory.getDAO("sale");
        
        RevenueSourceBO revSrcBO = revSrcDAO.create();
        
        TXLine.setRevSrc(revSrcBO);
        return TXLine;
    }//end addSaleToTXLine
    
    /**
     * this will add a product to a sale,
     * decrease the sale quantity or mark the sale as sold,
     * and calculate the sale's price
     * 
     * check to make sure there are sufficient items in the store_Product table
     * @param xid 
     * @param quantity 
     * @throws java.sql.SQLException 
     * @throws edu.byu.isys413.group1E.DataException 
     * @throws edu.byu.isys413.group1E.ConnectionPoolException 
     * @return 
     */
    public SaleBO addProductToSale(String xid, int quantity)throws SQLException, DataException, ConnectionPoolException{
        SaleDAO saleDAO = SaleDAO.getInstance();
        SaleBO saleBO = null;
        try{            
            saleBO = (SaleBO)saleDAO.create();
        }catch(Exception e){
            e.printStackTrace();
        }
        
        char first = xid.charAt(0);
        if(first == 'P'){
            ForSaleDAO forSaleDAO = ForSaleDAO.getInstance();
            ForSaleBO forSaleBO = (ForSaleBO)forSaleDAO.readBySKU(xid);
            
            saleBO.setProduct(forSaleBO);                         
            
        }//end forSale if
        if(first == 'C'){
            ConceptualDAO conceptualDAO = ConceptualDAO.getInstance();
            ConceptualBO conceptualBO = (ConceptualBO)conceptualDAO.readBySKU(xid);
            
            saleBO.setProduct(conceptualBO);            
            
        }//end conceptual if
                
        saleBO.setDescription(saleBO.getProduct().getProdType());
        saleBO.setPrice(saleBO.getProduct().getPrice());
        saleBO.setQuantity(quantity);
        saleBO.setReturnLine(false);
        
        return saleBO;
    }//end addProductToSale
    
    
    /**
     * 
     * @param saleBO 
     * @param session 
     * @throws edu.byu.isys413.group1E.DataException 
     * @throws edu.byu.isys413.group1E.ConnectionPoolException 
     * @throws edu.byu.isys413.group1E.SalesException 
     */
    public void postSale(SaleBO saleBO, Session session) throws DataException, ConnectionPoolException, SalesException{
        ConceptualDAO conceptualDAO = ConceptualDAO.getInstance();
        ConceptualBO conceptualBO = null;
        ForSaleDAO forSaleDAO = ForSaleDAO.getInstance();
        ForSaleBO forSaleBO = null;
        StoreBO storeBO = session.getStore();
        EmployeeBO employeeBO = session.getEmployee();
        StoreProductDAO storeProductDAO = StoreProductDAO.getInstance();
        StoreProductBO storeProductBO = null;
                
        if(saleBO.getReturnLine()==true){
        
            if(saleBO.getProduct().getProdType().equals("physical")){
                forSaleBO = (ForSaleBO)saleBO.getProduct();
                if(forSaleBO.getNewUsed()==false){
                    throw new SalesException("You can't return a used Item.");
                }//end catch for returning a used item
                forSaleBO.setSold(false);
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), forSaleBO.getConceptual().getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()+saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
                forSaleDAO.save(forSaleBO);                
            }//end posting of physical sale
            if(saleBO.getProduct().getProdType().equals("conceptual")){
                conceptualBO = (ConceptualBO)saleBO.getProduct();
                
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), conceptualBO.getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()+saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
            }//end posting of conceptual sale
            
        }if(saleBO.getReturnLine()==false){                    
            if(saleBO.getProduct().getProdType().equals("physical")){
                forSaleBO = (ForSaleBO)saleBO.getProduct();
                
                forSaleBO.setSold(true);
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), forSaleBO.getConceptual().getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()-saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
                forSaleDAO.save(forSaleBO);                
            }//end posting of physical sale
            if(saleBO.getProduct().getProdType().equals("conceptual")){
                conceptualBO = (ConceptualBO)saleBO.getProduct();
                
                storeProductBO = storeProductDAO.read(storeBO.getStoreID(), conceptualBO.getProductID());
                storeProductBO.setQtyOnHand(storeProductBO.getQtyOnHand()-saleBO.getQuantity());
                
                storeProductDAO.save(storeProductBO);
            }//end posting of conceptual sale
            
        }//end posting of sale
        
        
    }//end post sale
    
    /**
     * 
     * @param saleBO 
     * @param quantity 
     */
    public void changeSaleQuantity(SaleBO saleBO, int quantity){
        saleBO.setQuantity(quantity);       
    }//end changeSaleQuantity
    
    
    
}//end sale Handler
